package com.herench.bunch.emr.convert;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.group.ORU_R01_PATIENT_RESULT;
import ca.uhn.hl7v2.model.v24.message.ORU_R01;
import ca.uhn.hl7v2.model.v24.message.QBP_Q11;
import ca.uhn.hl7v2.model.v24.message.QRY_Q01;
import ca.uhn.hl7v2.model.v24.segment.*;
import com.herench.bunch.commons.support.RequestMessage;
import com.herench.bunch.commons.utils.IdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class VitalSignsConvert extends AbstractMessageConvert {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Map<String, Object> doToMessage(RequestMessage requestMessage) throws Exception {
        Map<String, Object> params = new HashMap<>();
        QRY_Q01 hl7Message = (QRY_Q01) requestMessage.getHl7Message();
        QRD qrd = hl7Message.getQRD();
        String patientId = qrd.getQrd4_QueryID().getValue();
        String startTime = qrd.getQrd1_QueryDateTime().getTs1_TimeOfAnEvent().getValue();
        String endTime = qrd.getQrd2_QueryFormatCode().getValue();
        params.put("patientId", patientId);
        params.put("startTime", startTime);
        params.put("endTime", endTime);
        return params;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Message fromMessage(Object message) throws Exception {
        Assert.notNull(message, "message 为空.");
        List<Map<String, Object>> res = (List<Map<String, Object>>) message;
        ORU_R01 oru_r01 = new ORU_R01();
        oru_r01.initQuickstart("ORU_R01", "ORU_R01", "P");
        MSH msh = oru_r01.getMSH();
        initResponseMessageMSH(msh);
        Map<String, Object> reqMap = res.get(0);

        ORU_R01_PATIENT_RESULT patient_result = oru_r01.getPATIENT_RESULT(0);

        PID pid = patient_result.getPATIENT().getPID();

        pid.getPid2_PatientID().getCx1_ID().setValue(String.valueOf(reqMap.get("patientId")));
        pid.getPid1_SetIDPID().setValue(String.valueOf(reqMap.get("visitId")));
        pid.getPid2_PatientID().getCx5_IdentifierTypeCode().setValue(String.valueOf(reqMap.get("babyNo")));

        for (int index = 0; index < res.size(); index++) {
            reqMap = res.get(index);
            OBX obx = patient_result.getORDER_OBSERVATION(0).getOBSERVATION(index).getOBX();

            obx.getObx12_DateLastObservationNormalValue().getTs1_TimeOfAnEvent().setValue(String.valueOf(reqMap.get("recordingDate")));
            obx.getObx14_DateTimeOfTheObservation().getTs1_TimeOfAnEvent().setValue(String.valueOf(reqMap.get("timePoint")));
            obx.getObx3_ObservationIdentifier().getCe2_Text().setValue(String.valueOf(reqMap.get("vitalSigns")));
            obx.getObx9_Probability(0).setValue(String.valueOf(reqMap.get("vitalSignsValues")));
            obx.getObx6_Units().getCe2_Text().setValue(String.valueOf(reqMap.get("units")));
            obx.getObx10_NatureOfAbnormalTest().setValue(String.valueOf(reqMap.get("vitalSignsValuesC")));
            obx.getObx3_ObservationIdentifier().getCe1_Identifier().setValue(String.valueOf(reqMap.get("vitalSignsNo")));
        }

        return oru_r01;
    }

    @Override
    public Message generatorReqMessage() throws Exception {
        QBP_Q11 qbp = new QBP_Q11();
        qbp.initQuickstart("QBP_Q11", "Q11", "L");
        MSH msh = qbp.getMSH();
        msh.getMsh3_SendingApplication().getHd1_NamespaceID().setValue("SSM");
        msh.getMessageControlID().setValue(IdGenerator.getUUID());
        msh.getMsh11_ProcessingID().getPt1_ProcessingID().setValue("200140");
        msh.getMsh11_ProcessingID().getProcessingMode().setValue("QR");
        QPD qpd = qbp.getQPD();
        qpd.getMessageQueryName().getIdentifier().setValue("查询号别");
        qpd.getQpd2_QueryTag().setValue("QR");
        return qbp;
    }
}
